package com.halfsword.creativecraft.entity.client;

import com.halfsword.creativecraft.entity.animations.NothingAnimationDefinitions;
import com.halfsword.creativecraft.entity.custom.NothingEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.model.HierarchicalModel;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.model.geom.PartPose;
import net.minecraft.client.model.geom.builders.*;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;

/**
 * 自定义实体 NothingEntity 的模型类
 *
 * DeepSeek 注释：此类定义了类似史莱姆的单体生物模型
 * 整个生物视为一个头部，旋转整个主体
 */
public class NothingModel<T extends Entity> extends HierarchicalModel<T> {
	private final ModelPart nothing; // 整个生物模型部件

	public NothingModel(ModelPart root) {
		// DeepSeek 注释：从根部件获取整个生物模型
		this.nothing = root.getChild("nothing");
	}

	public static LayerDefinition createBodyLayer() {
		MeshDefinition meshdefinition = new MeshDefinition();
		PartDefinition partdefinition = meshdefinition.getRoot();

		// DeepSeek 注释：创建主体模型部件
		PartDefinition nothing = partdefinition.addOrReplaceChild("nothing",
				CubeListBuilder.create()
						.texOffs(0, 0)
						.addBox(-6.0F, -5.0F, -6.0F, 12.0F, 5.0F, 12.0F, new CubeDeformation(0.0F)),
				// DeepSeek 注释：添加180度旋转修复方向问题
				PartPose.offsetAndRotation(0.0F, 24.0F, 0.0F, 0, (float)Math.PI, 0));

		// DeepSeek 注释：创建上部部件
		nothing.addOrReplaceChild("up",
				CubeListBuilder.create()
						.texOffs(0, 34).addBox(-7.0F, -8.0F, -1.0F, 14.0F, 2.0F, 2.0F, new CubeDeformation(0.0F))
						.texOffs(0, 17).addBox(-6.0F, -10.0F, -6.0F, 12.0F, 5.0F, 12.0F, new CubeDeformation(0.0F)),
				PartPose.offset(0.0F, 0.0F, 0.0F));

		return LayerDefinition.create(meshdefinition, 64, 64);
	}

	@Override
	public void setupAnim(T entity, float limbSwing, float limbSwingAmount,
						  float ageInTicks, float netHeadYaw, float headPitch) {
		// DeepSeek 注释：重置所有部件的姿势
		this.root().getAllParts().forEach(ModelPart::resetPose);

		// DeepSeek 注释：应用整个生物主体的旋转
		this.applyBodyRotation(netHeadYaw, headPitch, ageInTicks);
		this.animateWalk(NothingAnimationDefinitions.WALK,limbSwing,limbSwingAmount,2f,2f);
		this.animate(((NothingEntity) entity).idleAnimationState,NothingAnimationDefinitions.IDE,ageInTicks,2f);
		this.animate(((NothingEntity) entity).attackAnimationState,NothingAnimationDefinitions.DOEAT,ageInTicks,2f);

	}

	/**
	 * 应用生物主体旋转
	 *
	 * DeepSeek 注释：此方法控制整个生物（类似头部）的旋转角度
	 * 1. 限制旋转角度范围防止过度旋转
	 * 2. 将角度转换为弧度
	 * 3. 应用到整个生物模型
	 *
	 * @param netHeadYaw 偏航角（左右旋转）
	 * @param headPitch 俯仰角（上下旋转）
	 * @param ageInTicks 实体存在时间（刻）
	 */
	private void applyBodyRotation(float netHeadYaw, float headPitch, float ageInTicks) {
		// DeepSeek 注释：限制旋转角度范围（防止模型扭曲）
		netHeadYaw = Mth.clamp(netHeadYaw, -30.0F, 30.0F);    // 左右旋转限制在±30度
		headPitch = Mth.clamp(headPitch, -25.0F, 45.0F);      // 上下旋转限制在-25度到45度

		// DeepSeek 注释：将角度转换为弧度并应用到整个生物
		this.nothing.yRot = netHeadYaw * ((float)Math.PI / 180F); // Y轴旋转（左右看）
		this.nothing.xRot = headPitch * ((float)Math.PI / 180F);  // X轴旋转（上下看）
	}

	@Override
	public void renderToBuffer(PoseStack poseStack, VertexConsumer vertexConsumer,
							   int packedLight, int packedOverlay,
							   float red, float green, float blue, float alpha) {
		// DeepSeek 注释：渲染整个模型
		nothing.render(poseStack, vertexConsumer, packedLight, packedOverlay, red, green, blue, alpha);
	}

	@Override
	public ModelPart root() {
		// DeepSeek 注释：返回根部件
		return nothing;
	}
}